package org.itboys.core.service.mongo.web;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.entity.mongo.SundayArea;
import org.itboys.core.entity.mongo.SundayStore;
import org.itboys.core.entity.mongo.SundayVillage;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Author：贾琦磊
 * @Date：2018/8/1
 * @Description：web端_小区管理_service
 */
@Service
public class SundayVillageService extends BaseMongoService<SundayVillage>{

    @Autowired
    private SundayUploadService uploadService;
    @Resource(name = "mobileDs")
    private MongoDataSource mongoDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mongoDataSource;
    }
    @Override
    protected Class<SundayVillage> getEntityClass() {
        return SundayVillage.class;
    }
    @Autowired
    private SundayDistributionAreaService areaService;

    public void save(Long id, SundayVillage village, MultipartFile imageFile)throws Exception{
        Map<String, String> map = uploadService.uploadSingle(imageFile);
        if(StringUtils.isNotEmpty(map.get(SundayUploadService.UPLOAD_HALF_PATH))){
            village.setImage(map.get(SundayUploadService.UPLOAD_HALF_PATH));
        }
        if(LongUtil.isNotZreo(id)){
            super.updateExceptEmpty(id,village);
        }else {
            super.save(village);
        }
    }

    /**
     * web端分页获取
     * 精确查询根据省市区id
     * 模糊查询字段为名称
     * @param request
     * @return
     */
    public PageResult<SundayVillage> selectAll(HttpServletRequest request){
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request,"provinceId","cityId","districtId","storeId");
        Map<String, String> vagueParam = ParamUtil.packageMongoVagueParam(request, "name");
        param.put("isDeleted", SundayCommonConstants.NO);
        PageResult<SundayVillage> pageResult = super.containpageQuery(request, vagueParam, param);
        List<SundayVillage> villages = pageResult.getData();
        Set<Long> areaIds=new HashSet<>();
        for (SundayVillage village:villages
             ) {
            areaIds.add(village.getAreaId());
        }
        param.clear();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("id in",areaIds);
        List<SundayArea> areas = areaService.list(param);
        for (SundayVillage village:villages
                ) {
            for (SundayArea area:areas
                 ) {
                if(village.getAreaId().equals(area.getId())){
                    village.setAreaName(area.getName());
                }
            }
        }
        pageResult.setData(villages);
        return pageResult;
    }
    public SundayVillage findOne(Long id){
        SundayVillage village=new SundayVillage();
        if(LongUtil.isNotZreo(id)){
             village = super.getById(id);
            Map<String,Object> param=new HashMap();
            param.put("id",village.getAreaId());
            param.put("isDeleted",SundayCommonConstants.NO);
            List<SundayArea> list = areaService.list(param);
            if (ListUtil.isNotNull(list)){
                village.setAreaName(list.get(0).getName());
            }
        }
        return village;
    }
}
